package com.ftd.daoImp;

import com.ftd.config.ResultType;
import com.ftd.dao.UserDao;
import com.ftd.model.User;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("UserDaoImp")

public class UserDaoImp extends BaseHibernateDaoImp  implements UserDao {

	Session session;

	@Autowired
	private User user;

	@Override
	public ResultType register(String userPhone, String userPassword, String userEmail){

		//判断该手机号是否注册过
		if(getUserByPhone(userPhone) != null){
			return ResultType.HAS_BEEN_REGISTERED;
		}  else {
			try{
				session = getSession();
				//由于采用spring注入,所以每个user都是同一个,总是对同一个insert会出错,所以要用new
				//spring默认是单例模式,可以尝试scope=prototype
				user = new User.Builder(userPhone,userPassword,userEmail).bulid();
				user.setUserPhone(userPhone);
				user.setUserPassword(userPassword);
				user.setUserEmail(userEmail);
				session.persist(user);
				//因为事务管理已经交给spring了,自己就不用commit了
//			session.getTransaction().commit();

			} catch (Exception e){
				e.printStackTrace();
			}

			return ResultType.SUCCESS_ALL;
		}

	}

	@Override
	public User queryById(int id) {
		User user = null;
		try{
			session = getSession();
			user = session.get(User.class,id);

		} catch (Exception e){
			e.printStackTrace();
		} finally {
		}
		return user;
	}
	@Override
	public List queryAll(){
		List<User> userList = null;
		String hql;
		try{
			session = getSession();
			hql = "from User";
			userList  = session.createQuery(hql,User.class).list();
		} catch (Exception e){
			e.printStackTrace();
		} finally {
		}
		return userList;
	}

	@Override
	public User getUserByPhone(String userPhone){
		session = getSession();
		String hql = "from User u where userPhone=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,userPhone);
		if (query.list().size()>0){
			return (User)query.list().get(0);
		} else {
			return null;
		}


	}
	@Override
	public ResultType findPass(String userPhone, String userEmail,String oldPass, String newPass) {
		session = getSession();
		String hql = "from User u where userPhone=?";
		Query query = session.createQuery(hql);
		User user = (User)query.setParameter(0,userPhone).list().get(0);
		if (user != null){
			if (userEmail.equals(user.getUserEmail())
					&& oldPass.equals(user.getUserPassword()) ){
				user.setUserPassword(newPass);
				return ResultType.SUCCESS_ALL;
			} else {
				return ResultType.WRONG_ALL;
			}
		} else {
			return ResultType.WRONG_USERNAME;
		}

	}

	@Override
	public ResultType login(String userPhone ,String userPassword){
		session = getSession();
		if (getUserByPhone(userPhone) != null){
			if (userPassword.equals(getUserByPhone(userPhone).getUserPassword())){
				return ResultType.SUCCESS_ALL;
			} else {
				return ResultType.WRONG_PASSWORD;
			}
		} else {
			return ResultType.WRONG_USERNAME;
		}
	}

}